package com.econ.powercloud.ems.service.common;

import com.econ.powercloud.datahandle.facade.domain.ProcessedRunData;
import com.econ.powercloud.ems.common.enums.DitTypeEnum;
import com.econ.powercloud.ems.common.enums.MeasureType;
import com.econ.powercloud.ems.pojo.FlashDataVO;
import com.econ.powercloud.ems.pojo.QueryDataVO;
import com.econ.powercloud.ems.service.biz.scene.CacheBizService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author : zje
 * @Description : 实时数据查询封装
 * @Date : 2021/9/14 16:48
 * @Modified by : zjf
 */
@Service
public class QueryFlashDataCommonService {

    @Autowired
    private CacheBizService cacheBizService;

    /**
     * 批量实时数据查询
     */
    public List<FlashDataVO> getFlashData(List<QueryDataVO> queryDataVOS){
        List<FlashDataVO> flashDataVOList = Lists.newLinkedList();
        if(CollectionUtils.isEmpty(queryDataVOS)){
            return flashDataVOList;
        }
        Map<String, QueryDataVO> queryDataVOMap = queryDataVOS.stream().collect(Collectors.toMap(QueryDataVO::getPid, d->d,(p1, p2)->p1));
        //按照initDeviceCode分组
        Map<String,List<QueryDataVO>> queryDataMap =  queryDataVOS.stream().collect(Collectors.groupingBy(d -> d.getInitDeviceCode()));
        queryDataMap.forEach((k,v)->{
            List<String> dits = v.stream().map(QueryDataVO::getDit).collect(Collectors.toList());
            Map<String, ProcessedRunData> deviceRunDataMap = cacheBizService.getDeviceRunDataMap(k, dits);
            if(!deviceRunDataMap.isEmpty()){
                deviceRunDataMap.values().stream().filter(Objects::nonNull).forEach(f->{
                    QueryDataVO queryDataVO = queryDataVOMap.get(f.getPid());
                    flashDataVOList.add(FlashDataVO.builder()
                            .pid(f.getPid())
                            .deviceCode(queryDataVO.getDeviceCode())
                            .initDeviceCode(queryDataVO.getInitDeviceCode())
                            .dit(queryDataVO.getDit()).val(f.getVal())
                            .unit(f.getUnit()).remark(f.getRemark())
                            .time(f.getTime())
                            .measureType(MeasureType.fromCodeInIot(f.getType()))
                            .ditType(MeasureType.yx.getCodeInIot().equals(f.getType())? DitTypeEnum.YX.getCode() : DitTypeEnum.YC.getCode())
                            .build());
                });
            }
        });
        return flashDataVOList;
    }

    /**
     * 根据设备i与点号后缀单条实时数据查询
     */
    public FlashDataVO getFlashDataForDeviceCode(String deviceCode, String dit, Integer measureType){
        Map<String, ProcessedRunData> deviceRunDataMap = cacheBizService.getDeviceRunDataMap(deviceCode, Lists.newArrayList(dit));
        String pid = deviceCode + "."+ dit;
        if(deviceRunDataMap.get(pid) == null){
            return FlashDataVO.builder().pid(pid).initDeviceCode(deviceCode)
                    .dit(dit).measureType(measureType).build();
        }
        ProcessedRunData processedRunData = deviceRunDataMap.get(pid);
        return FlashDataVO.builder()
                .pid(processedRunData.getPid()).initDeviceCode(deviceCode)
                .dit(dit).val(processedRunData.getVal())
                .unit(processedRunData.getUnit()).remark(processedRunData.getRemark())
                .time(processedRunData.getTime()).measureType(measureType)
                .ditType(MeasureType.yx.getCodeInIot().equals(processedRunData.getType())? DitTypeEnum.YX.getCode() : DitTypeEnum.YC.getCode())
                .build();
    }

}
